package com.example.administrator.capital;


import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.capital.base.BaseMainFragment;
import com.example.administrator.capital.consult.ConsultFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.eventbus.TabSelectedEvent;
import com.example.administrator.capital.home.HomeFragment;
import com.example.administrator.capital.item.ItemFragment;
import com.example.administrator.capital.mine.MineFragment;
import com.example.administrator.capital.widget.BottomBar;
import com.example.administrator.capital.widget.BottomBarTab;

import org.greenrobot.eventbus.Subscribe;

import eventbusactivityscope.EventBusActivityScope;
import fragmentation.SupportFragment;


/**
 * A simple {@link Fragment} subclass.
 *
 */
public class MainFragment extends SupportFragment {

    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOUR = 3;
    private SupportFragment[] mFragments = new SupportFragment[3];
    private BottomBar mBottomBar;
    public MainFragment() {
        // Required empty public constructor


    }
    public static MainFragment newInstance() {

        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusActivityScope.getDefault(_mActivity).register(this);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportFragment firstFragment = findChildFragment(HomeFragment.class);
        if (firstFragment == null) {
            mFragments[FIRST] = HomeFragment.newInstance();
            mFragments[SECOND] = ItemFragment.newInstance();
//            mFragments[THIRD] = ConsultFragment.newInstance();
            mFragments[THIRD] = MineFragment.newInstance();
            //添加tab中的fragment
            loadMultipleRootFragment(R.id.fl_tab_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],mFragments[THIRD]);
        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            // 这里我们需要拿到mFragments的引用
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findChildFragment(ItemFragment.class);
//            mFragments[THIRD] = findChildFragment(ConsultFragment.class);
            mFragments[THIRD] = findChildFragment(MineFragment.class);


        }
    }
    private void initView(View view) {
        mBottomBar = (BottomBar) view.findViewById(R.id.bottomBar);
//添加下标tab
        mBottomBar
                .addItem(new BottomBarTab(_mActivity, R.drawable.home, getString(R.string.home)))
                .addItem(new BottomBarTab(_mActivity, R.drawable.project, getString(R.string.item)))
//                .addItem(new_name BottomBarTab(_mActivity, R.drawable.information, getString(R.string.consult)))
                .addItem(new BottomBarTab(_mActivity, R.drawable.mine, getString(R.string.mine)));
        // 模拟未读消息
//        mBottomBar.getItem(SECOND).setUnreadCount(9);
        mBottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position], mFragments[prePosition]);
//                BottomBarTab tab = mBottomBar.getItem(FIRST);
//                if (position == FIRST) {
//                    tab.setUnreadCount(tab.getUnreadCount());
//                } else {
//                    tab.setUnreadCount(tab.getUnreadCount() + 1);
//                }
            }
            @Override
            public void onTabUnselected(int position) {
            }
            @Override
            public void onTabReselected(int position) {
                // 在FirstPagerFragment,FirstHomeFragment中接收, 因为是嵌套的Fragment
                // 主要为了交互: 重选tab 如果列表不在顶部则移动到顶部,如果已经在顶部,则刷新
//                EventBusActivityScope.getDefault(_mActivity).post(new_name TabSelectedEvent(position));
            }
        });
    }
    @Subscribe
    public void onTabSelectedEvent(TabSelectedEvent event) {
        switch (event.position) {
            case 1:
                mBottomBar.setCurrentItem(1);
                break;
        }
    }
    @Subscribe
    public void onTabSelectedEvent(EventBase event) {
        if (event.getName().equals("回到首页")) {
            mBottomBar.setCurrentItem(0);
        }

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
         /*eventbus注销*/
        EventBusActivityScope.getDefault(_mActivity).unregister(this);
    }
    /**
     * start other BrotherFragment
     */
    public void startBrotherFragment(SupportFragment targetFragment) {
        start(targetFragment);
    }
}
